unit ChangeEkAndDic;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, IBSQL;

type
  TfmChangeEkAndDic = class(TForm)
    Label1: TLabel;
    lblOldValue: TLabel;
    Label2: TLabel;
    edtNewValueID: TEdit;
    lblNewValue: TLabel;
    btnChange: TButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure edtNewValueIDKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure btnChangeClick(Sender: TObject);
  private
    FDicName: string;
    FEntityName: string;
    FOldValueId: integer;
    procedure SetOldValueId(const Value: integer);
    { Private declarations }
  public
    { Public declarations }
    constructor Create(aOwner: TComponent; aEntityName: string);
    property OldValueId: integer write SetOldValueId;
  end;


implementation

uses DicDM;

{$R *.DFM}

constructor TfmChangeEkAndDic.Create(aOwner: TComponent;
  aEntityName: string);
begin
  inherited Create(aOwner);
  FEntityName := aEntityName;
  FDicName := FEntityName + '_DIC';
end;

procedure TfmChangeEkAndDic.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfmChangeEkAndDic.FormCreate(Sender: TObject);
begin
  lblNewValue.Caption := '';
  edtNewValueId.Text := '';
  btnChange.Enabled := False;
end;

procedure TfmChangeEkAndDic.SetOldValueId(const Value: integer);
var q: Tibsql;
begin
    FOldValueId := Value;
    q := dmDic.CreateIBSQL(Self);
    q.Transaction.StartTransaction;
    q.SQL.Text := Format('SELECT * FROM %s WHERE ID = :ID', [FDicName]);
    q.ParamByName('ID').AsInteger := Value;
    q.ExecQuery;
    if not q.Eof then
      lblOldValue.Caption := Format('%s (ID %d)',
        [q.FieldByName('NAME').AsString, Value])
    else
      lblOldValue.Caption := '';
    q.Transaction.Commit;
    q.Free;
end;

procedure TfmChangeEkAndDic.edtNewValueIDKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
var q: TIBSQL;
begin
  if Key = VK_RETURN then begin
    q := dmDic.CreateIBSQL(Self);
    q.Transaction.StartTransaction;
    q.SQL.Text := Format('SELECT * FROM %s WHERE ID = :ID', [FDicName]);
    q.ParamByName('ID').AsString := edtNewValueId.Text;
    q.ExecQuery;
    if not q.Eof then begin
      lblNewValue.Caption := q.FieldByName('NAME').AsString;
      btnChange.Enabled := True;
    end
    else begin
      lblNewValue.Caption := '';
      btnChange.Enabled := False;
    end;
    q.Transaction.Commit;
    q.Free;
  end;
end;

procedure TfmChangeEkAndDic.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_ESCAPE then Close;
end;

procedure TfmChangeEkAndDic.btnChangeClick(Sender: TObject);
var q: Tibsql;
begin
    q := dmDic.CreateIBSQL(Self);
    q.Transaction.StartTransaction;
    q.SQL.Text := Format('UPDATE EK_AND_%s SET %0:s_ID = :NEW_ID WHERE %0:s_ID = :OLD_ID',
                          [FEntityName]);
    q.ParamByName('OLD_ID').AsInteger := FOldValueId;
    q.ParamByName('NEW_ID').AsString := edtNewValueId.Text;
    q.ExecQuery;
    q.Transaction.Commit;
    q.Free;
    ModalResult := mrOk;
end;

end.
